﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Threading;

/// <summary>
/// Summary description for FundSource
/// </summary>
public class FundSource
{
    public FundSource()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public FundPrice generate(string fundCode, decimal baseDecimal)
    {
        FundPrice fundPrice = new FundPrice();
        
        Random random = new Random();

       
        Double randomValue = random.NextDouble();
        randomValue = (randomValue * 1000 / 999);

        fundPrice.fundCode = fundCode;
        fundPrice.offerPrice = baseDecimal + System.Convert.ToDecimal(randomValue);
        //Bid-Offer Spread is 5
        fundPrice.bidPrice = fundPrice.offerPrice * 0.95m;
        fundPrice.valuationDate = DateTime.Now;
        fundPrice.currency = "SGD";

        return fundPrice;
    }

    public void generateAllFundPrices()
    {
        var conn = DBManager.getSqlConnection();
        conn.Open();
        string sql = "select fund_code,base_random_price from R_FUND";
        var cmd = new SqlCommand(sql, conn);
        
        SqlDataReader reader = cmd.ExecuteReader();
        
        while (reader.Read())
        {
            string fundCode = reader.GetString(0);
            if (haveBeenInsert(fundCode))
            {
                continue;
            }

            FundPrice fundPrice = generate(fundCode, reader.GetDecimal(1));
           
            fundPrice.save();

        }
        reader.Close();

        conn.Close();

    }

    private bool haveBeenInsert(string fundCode)
    {
        var conn = DBManager.getSqlConnection();
        conn.Open();

        string sql = "select 1 from R_FUND_DAILY_PRICE where fund_code='"+fundCode
            + "' and CONVERT(VARCHAR(8),valuation_date,103) = CONVERT(VARCHAR(8),GETDATE(),103)";
        var cmd = new SqlCommand(sql, conn);
        SqlDataReader reader = cmd.ExecuteReader();

        try
        {
            if (reader.Read())
            {
                return true;
            }
        }catch(Exception e){
            throw e;
        }finally{
            reader.Close();
            conn.Close();
        }

        return false;
    
    }

    public void checkDailyPrice()
    {
        Console.WriteLine("Run checkDailyPrice");
        ThreadStart childthreat = new ThreadStart(childthreadcall);
        Thread child = new Thread(childthreat);
        child.Start();
    }

    public void childthreadcall()
    {
        try
        {
            while(true){
                Console.WriteLine("Run generateAllFundPrices");
                generateAllFundPrices();
                Thread.Sleep(1000 * 60*2);
            }    
         }
        catch (ThreadAbortException e)
        {
            Console.WriteLine(e);
            throw e;
        }
        finally
        {
            ;
        }
    }

    static void Main()
    {
        new FundSource().generateAllFundPrices();
    }

}